package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.jsonVo.PrimarySureJsonVo;
import top.infopub.mgr.bpa.service.BpaPrimarySureService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.vo.BpaPrimarySureVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.model.bpa.PrimarySure;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * Created by wurz on 2021/6/11.
 */
@Controller
@RequestMapping("/primarySure")
public class PrimarySureController extends GenericFlowController {
    @Autowired
    private BpaPrimarySureService primarySureService;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-primarySure-list";
    }

    /**
     * 设备合同列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BpaPrimarySureVo> queryDataByCondition(BpaPrimarySureVo sureVo,
                                                        ParamPage paramPage,
                                                        String monthStartDateStr,
                                                        String monthEndDateStr) throws ParseException {
        Page<BpaPrimarySureVo> page = new Page<BpaPrimarySureVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
//        //查询时，结束日期增加一天
//        if (sureVo.getEndDate() != null) {
//            sureVo.setEndDate(DateUtils.addDays(sureVo.getEndDate(), 1));
//        }
//        //查询时，创效月份开始日期
//        if (StringUtils.isNotEmpty(monthStartDateStr)) {
//            sureVo.setMonthStartDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
//        }
//        //查询时，创效月份结束日期增加一天
//        if (StringUtils.isNotEmpty(monthEndDateStr)) {
//            sureVo.setMonthEndDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
//                    monthEndDateStr, "yyyy-MM")));
//        }
        page = primarySureService.selectList(page,sureVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 投建项目id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        if (StringUtils.isNotBlank(id)) {
            //修改成主表数据
            PrimarySure priSure = primarySureService.selectOne(id);
            if (priSure != null){
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), priSure.getProcessStatus())
                        || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), priSure.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                    if (StringUtils.isNotBlank(priSure.getProcessId()) && priSure.getProcessId().length() > 10) {
                        request.setAttribute("processId", priSure.getProcessId());
                        String auditTabFlag = bpaProcessManageService.checkShowAuditTab(priSure.getProcessId(), vo);
                        if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), priSure.getProcessStatus())
                                && StringUtils.equals("0", auditTabFlag)) {
                            request.setAttribute("isAuditFlag", "0");
                        }
                        request.setAttribute("auditTabFlag", auditTabFlag);
                    }
                }
            }
            request.setAttribute("proCode", proCode);
        }
        //统一一个详情，根据主表返回的类型显示不同的数据--前端会处理，后端只要返回标识
        return "bpa/lay-primarySure-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetailById(String id) {
        PrimarySureJsonVo priSureJson = primarySureService.queryDetailById(id);
        return RspVo.getSuccessResponse(priSureJson);
    }

    /**
     * 根据项目编码查询项目基础信息
     * @param proCode
     * @return
     */
    @RequestMapping("/queryProInfoByProCode")
    @ResponseBody
    public RspVo queryProInfoByProCode(String proCode) {
        PrimarySureJsonVo priSureJson = primarySureService.queryProInfoByProCode(proCode);
        return RspVo.getSuccessResponse(priSureJson);
    }

    /**
     * 根据项目编码和确权类型查询对应合同列表
     * @param proCode
     * @param sureType
     * @return
     */
    @RequestMapping("/queryContractInfo")
    @ResponseBody
    public RspVo queryContractInfo(String proCode, String sureType) {
        PrimarySureJsonVo priSureJson = primarySureService.queryContractInfo(proCode, sureType);
        return RspVo.getSuccessResponse(priSureJson);
    }

    /**
     * 新增/编辑
     * @param bodyStr
     * @param submitType
     * @return
     */
    @RequestMapping("/edit")
    public @ResponseBody RspVo edit(String bodyStr, Integer submitType) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        PrimarySureJsonVo priSureJson = JSON.parseObject(str, PrimarySureJsonVo.class);

        //传入参数自己看情况
        RspVo rspVo = primarySureService.edit(priSureJson, submitType, vo);
        return rspVo;
    }
}
